﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace PeisongWeb
{
    public partial class buyer_reportPayableStatistic : buyerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CC", 1))
                {
                    drpBind();
                    showNotPayable();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "buyer_index.html");
                    return;
                }
            }
        }

        void drpBind()
        {
            int byear = DateTime.Now.Year;
            int bmonth = DateTime.Now.Month + 1;
            for (int i = 2009; i <= 2012; i++)
            {
                ddl_Year.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                ddl_Month.Items.Add(i.ToString());
            }
            ddl_Year.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-请选择-", ""));
            ddl_Month.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-请选择-", ""));
        }
        private void showNotPayable()
        {
            DataRow dr = logic.paymentHistory.listWithExtend(logic.customer.getCompanyId()).Rows[0];
            int pmtNum = Convert.ToInt32(dr["pmtNum"]);
            if (pmtNum != 0)
            {
                string money = string.Format("{0:F2}", dr["sumAmount"]);
                sp_Num.InnerText = dr["pmtNum"].ToString();
                sp_total.InnerText = sp_total2.InnerText = money;
                sp_Day.InnerText = dr["syDay"].ToString();
                if (Convert.ToInt32(dr["syDay"]) <=0)
                {
                    li_day.Visible = false;
                    li_chao.Visible = true;
                }
            }
            else
            {
                dv_payable.Visible = false;
            }
        }
        void rptBind()
        {
            int buyerId = logic.customer.getCompanyId();
            StringBuilder where = new StringBuilder();
            where.Append(" companyId=" + buyerId);
            if (ddl_Year.SelectedValue != "")
                where.AppendFormat(" and year(updateStatusDt)={0}", ddl_Year.SelectedValue);
            if (ddl_Month.SelectedValue != "")
                where.AppendFormat(" and month(updateStatusDt)={0}", ddl_Month.SelectedValue);
            if (ddl_isPay.SelectedValue != "")
                where.AppendFormat(" and paystatus ='{0}' ", ddl_isPay.SelectedValue.ToString());
            DataSet ds = logic.order.listWithPayable(where.ToString(), "orderid desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex); ;
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
            showCollect();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void btn_select_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        private void showCollect()
        {
            int companyId = logic.customer.getCompanyId();
            DateTime? startDt = null;
            DateTime? endDt = null;

            if(ddl_Year.SelectedValue!=""&&ddl_Month.SelectedValue!="")
            {
                startDt = Convert.ToDateTime(ddl_Year.SelectedValue + "-" + ddl_Month.SelectedValue + "-" + "01");
                endDt = Convert.ToDateTime(startDt).AddMonths(1);
            }
            else if(ddl_Year.SelectedValue!="")
            {
                startDt = Convert.ToDateTime(ddl_Year.SelectedValue + "-" + "01" + "-" + "01");
                endDt = Convert.ToDateTime(startDt).AddYears(1);
            }
            DataRow dr = logic.paymentHistory.listWithCollect(companyId, startDt, endDt,"1").Rows[0];
            lit_orderNum.Text = string.Format("{0:F2}", dr["orderNum"]);
            lit_payMoney.Text = string.Format("{0:F2}", dr["payMoney"]);
            lit_notPayMoney.Text = string.Format("{0:F2}", dr["notPayMoney"]);
            lit_total.Text = string.Format("{0:F2}", dr["orderAmount"]);
        }
        protected void lbn_searchNotPay_Click(object sender, EventArgs e)
        {
            ddl_isPay.SelectedValue = "1";
            showNotPayable();
            rptBind();
        }
    }
}
